/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dao.ImportExportBillDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ImportExportBillModel;

/**
 *
 * @author ThanhToan
 */
public class IEBillController extends HttpServlet {
   private String URL = "";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("action") != null && request.getParameter("action").toString().equals("review"))
	{
            review(request, response);
	}
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

     private void review(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
         try {
             String ieBillID = request.getParameter("ieBillID").toString();
             ImportExportBillModel obj = new ImportExportBillModel();
             obj = ImportExportBillDAO.getIEBillByID(ieBillID);
             if(request.getParameter("approve") != null && request.getParameter("approve").toString().equals("approve")){
                obj.setStatus("1");
             }
             if(request.getParameter("reject") != null && request.getParameter("reject").toString().equals("reject")){
                obj.setStatus("2");
             }
             obj.setApprovedBy(request.getSession().getAttribute("userID").toString());
             if(request.getParameter("reason")!=null){
                obj.setRejectReason(request.getParameter("reason").toString());
             }
             if(ImportExportBillDAO.reviewIEBill(obj)){
                URL = "ieBill.jsp?msg='update'";
             }
             RequestDispatcher rd = request.getRequestDispatcher(URL);
             rd.forward(request, response);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
}
